using System;
using System.IO;
using com.youzu.warh.protocol;
using ProtoBuf;
using UnityEngine;
using UnityEngine.Profiling;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Managers.NetworkV2;
using Yoozoo.Managers.NetworkV2.Core;
using Yoozoo.Managers.NetworkV2.DataStruct;
using Yoozoo.Mars.Got;

namespace Yoozoo.Framework.Network.Helpers
{
    public class CSharpNetWorkReciever : Singleton<CSharpNetWorkReciever>
    {
        const int REVIVE_BUFFER_LEN = 256 * 1024;
        
        byte[] reciveBuffer = new byte[REVIVE_BUFFER_LEN];
        MemoryStream msRecive;
        private ProtoBufSerializer protoBufSerial;
        public void Initialize()
        {
            protoBufSerial = ProtoBufSerializer.GetInstance();
            protoBufSerial.Initialize();
            msRecive = new MemoryStream(reciveBuffer, 0, REVIVE_BUFFER_LEN, true, true);
            
            var channel = UMTNetwork.GetChannel("mainChannel");
            if (channel!=null)
            {
                channel.OnCSharpNetworkChannelReceivePacket += OnOnNetworkChannelReceivePacket;
            }
        }
        
        private void OnOnNetworkChannelReceivePacket(NetChannel channel, IPacket packet)
        {
            //在这里解析C#端协议
            if (packet.Id == 1000)
            {
                Profiler.BeginSample("SCRoundFight Deserialize");
                SCRoundFight roundFight = Deserialize(packet.rawData);
                Profiler.EndSample();

                GTAGameStart.eventManager.TriggerEvent(ClientEvent.RoundFight,roundFight);
                // 当部队没注册的时候 直接发送更新
                if (MarchManager.Instance == null)
                {
                    MarchManager.OnRoundFightInCity(roundFight);
                }
                ProtoFactory.Recycle(roundFight);
            }
            else if (packet.Id == 1001)
            {
                //RtsArenaManager.Instance.OnGetReport(packet.rawData);
                //RtsArenaManager.Instance.PrepareBattle(report.reportSummary);
            }
            else if (packet.Id == 1002)
            {
                #if UNITY_EDITOR
                Debug.Log("收到远征战报");
                #endif
                SCExpeditionBattleReport report = DeserializeExpeditionReport(packet.rawData);
                RtsArenaManager.Instance.OnReportReady(report.report);
            }
        }
        
        public SCRoundFight Deserialize(byte[] message)
        {
            msRecive.Seek(0, SeekOrigin.Begin);
            Buffer.BlockCopy(message, 0, msRecive.GetBuffer(), 0, message.Length);
            var scRoundTemp = ProtoBufSerializer.Deserialize(msRecive, typeof(SCRoundFight),message.Length) as SCRoundFight;
            return scRoundTemp;
        }

        public SCExpeditionBattleReport DeserializeExpeditionReport(byte[] message)
        {
            if (msRecive.Length <= message.Length)
            {
                msRecive.Close();
                var length = Mathf.CeilToInt(message.Length / 1024f) * 1024;
                reciveBuffer = new byte[length];
                msRecive = new MemoryStream(reciveBuffer, 0, length, true, true);
            }
            msRecive.Seek(0, SeekOrigin.Begin);
            Buffer.BlockCopy(message, 0, msRecive.GetBuffer(), 0, message.Length);
            var scRoundTemp = ProtoBufSerializer.Deserialize(msRecive, typeof(SCExpeditionBattleReport),message.Length) as SCExpeditionBattleReport;
            return scRoundTemp;
        }
        
    }
}